#import "DLog.h"


/*
 把view阶层生成一个树
 */
//一个包含所有子view的列表(树形)
NSArray *allSubviews(UIView *aView)
{
	NSArray *results = [aView subviews];
	for (UIView *eachView in [aView subviews])
	{
		NSArray *riz = allSubviews(eachView);
		if (riz) results = [results arrayByAddingObjectsFromArray:riz];
	}
	return results;
}

// Return all views throughout the application
NSArray *allApplicationViews()
{
    NSArray *results = [[UIApplication sharedApplication] windows];
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
	{
		NSArray *riz = allSubviews(window);
        if (riz) results = [results arrayByAddingObjectsFromArray: riz];
	}
    return results;
}

//当前view的路径
NSArray *pathToView(UIView *aView)
{
    NSMutableArray *array = [NSMutableArray arrayWithObject:aView];
    UIView *view = aView;
    UIWindow *window = aView.window;
    while (view != window)
    {
        view = [view superview];
        [array insertObject:view atIndex:0];
    }
    return array;
}

NSString *getDateString(NSDate *date)
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"]; //自定义格式
    NSString *friendlyDate = [formatter stringFromDate:date];
    [formatter release];
    return friendlyDate;
}

char* myitoa(int n, char* str, int radix){
    int a = n;  //被除数
    int b = 0;  //余数
    int c = 1;  //正负数
    char* p = str;
    
    if (0 == a){                  // 如果是0，直接输出"0"
        strcpy(str, "0");
        return str;
    }
    else if (a < 0){              // 如果是负数，记下负号
        c = -1;
        a = -a;
    }
    
    while(a > 0){                 // 反复取余
        b = a % radix;
        a = a / radix;
        
        if (b < 10) 
            *p = b + '0';             // 0-9 直接输出"0"-"9"
        else
            *p = b - 10 + 'A';        // > 10 输出"A"-...
        
        p++;
    }
    
    if (c < 0) *p++ = '-';        // 按需添加负号
    *p = 0;                       // 字符串结尾
    
    // 下面将字符串 str 逆序即可，也可调用系统函数 str = strrev(str);
    int len = strlen(str);        // 取出长度
    int i = 0;    
    for (i = 0; i < len / 2; i++){// 首尾交换，共计 len / 2 次
        c = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = c;
    }
    
    return str;
}

void print0(id formatstring,...)
{
    //格式化字符串
    NSString* outstring = nil;
    if (formatstring) {
        va_list arglist;
        va_start(arglist, formatstring);
        outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        va_end(arglist);
    }
    else {
        outstring = [[NSString alloc] initWithString:@""];
    }
    printf("%s\n", [outstring UTF8String]);
    [outstring release];
}

void print1(const char* function, int line, id formatstring,...)
{
    //格式化字符串
    NSString* outstring = nil;
    if (formatstring) {
        va_list arglist;
        va_start(arglist, formatstring);
        outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        va_end(arglist);
    }
    else {
        outstring = [[NSString alloc] initWithString:@""];
    }
    
    //函数(行数) 日志内容
    printf("%s(%d) %s\n", function, line, [outstring UTF8String]);
    
    [outstring release];
}

void print2(const char* filename, const char* function, int line, id formatstring,...)
{
    //格式化字符串
    NSString* outstring = nil;
    if (formatstring) {
        va_list arglist;
        va_start(arglist, formatstring);
        outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        va_end(arglist);
    }
    else {
        outstring = [[NSString alloc] initWithString:@""];
    }
    
    //文件名
    NSString* file = [[NSString alloc] initWithFormat:@"%s", filename];
    
    //文件名 函数(行数) 日志内容
    printf("%s %s(%d) %s\n", [[file lastPathComponent] UTF8String], function, line, [outstring UTF8String]);
    
    [outstring release];
    [file release];
}

void print3(const char* filename, const char* function, int line, id formatstring,...)
{
    //格式化字符串
    NSString* outstring = nil;
    if (formatstring) {
        va_list arglist;
        va_start(arglist, formatstring);
        outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        va_end(arglist);
    }
    else {
        outstring = [[NSString alloc] initWithString:@""];
    }
    
    //时间
    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss:SSS"]; //自定义格式
    NSString *friendlyDate = [formatter stringFromDate:now];
    
    //文件名
    NSString* file = [[NSString alloc] initWithFormat:@"%s", filename];
    
    //时间 文件名 函数(行数) 日志内容
    printf("%s %s %s(%d) %s\n", [friendlyDate UTF8String], [[file lastPathComponent] UTF8String], function, line, [outstring UTF8String]);
    
    [outstring release];
    [formatter release];
    [file release];
}

void print4(const char* filename, const char* function, int line, id formatstring,...)
{
    //格式化字符串
    NSString* outstring = nil;
    if (formatstring) {
        va_list arglist;
        va_start(arglist, formatstring);
        outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
        va_end(arglist);
    }
    else {
        outstring = [[NSString alloc] initWithString:@""];
    }
    
    //日期、时间
    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"]; //自定义格式
    NSString *friendlyDate = [formatter stringFromDate:now];
    
    //文件名
    NSString* file = [[NSString alloc] initWithFormat:@"%s", filename];
    
    //日期 时间 文件名 函数(行数) 日志内容
    printf("%s %s %s(%d) %s\n", [friendlyDate UTF8String], [[file lastPathComponent] UTF8String], function, line, [outstring UTF8String]);
    
    [outstring release];
    [formatter release];
    [file release];
}

NSString* hexdump(NSData *data, NSString* prompt)
{
    const unsigned char *ptr = (const unsigned char *)[data bytes];
    int buflen = [data length];
        
    //结果字符串
	NSMutableString* str = [[NSMutableString alloc]initWithCapacity:512];;
    //添加外边传进来的字符串
    [str appendString:prompt];
    //添加data的长度
    [str appendFormat:@"\nthe buf length:%d\n", buflen];
	if(!ptr || buflen==0)
    {
        [str appendString:@"the buf is NULL!!"];
        return [str autorelease];
    }
    
    //每行打印linecount个字符
    const int linecount = 15;
	unsigned char *buf = (unsigned char*)ptr;
	int i, j;
	for (i=0; i<buflen; i+=linecount) 
    {
        //行头打印指针
        [str appendFormat:@"%08x: ", buf+i];
        //行的前半部分，分别打印每个字符的十六进制
		for (j=0; j<linecount; j++) 
        {
            if (i+j < buflen)
			{
                [str appendFormat:@"%02x ", buf[i+j]];
			}
            else 
            {
                [str appendString:@"   "];
            }
        }
        [str appendString:@"    "];
        
        //行的后半部分打印可见字符
        for (j=0; j<linecount; j++) 
        {
            if (i+j < buflen)
            {
                [str appendFormat:@"%c", isprint(buf[i+j]) ? buf[i+j] : '.'];
            } 
        }            
        [str appendString:@"\n"];
	}
	return [str autorelease];
}
